package com.guojian.common.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class DateUtilsx {

	public static final String YMD_PATTERN = "yyyy-MM-dd";

	public static final String YMD_PATTERN_C = "yyyy年MM月dd日";

	public static final String YM_PATTERN = "yyyy-MM";

	public static final String Y_PATTERN = "yyyy";

	public static final String YM_PATTERN_C = "yyyy年MM月";

	public static final String YMDHMS_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static final String YMDHMS_PATTERN1 = "yyyy/MM/dd HH:mm:ss";

	public static final String YMD_NO_PATTERN = "yyyyMMdd";

	public static final String HH_PATTERN = "HH";

	public static final String DD_PATTERN = "dd";

	public static final String MM_PATTERN = "MM";

	public static boolean compare(String date1, String date2, String parsePattern) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {

			if (StringUtils.isNotBlank(date1)) {
				c1.setTime(DateUtils.parseDate(date1, parsePattern));
			}
			if (StringUtils.isNotBlank(date2)) {
				c2.setTime(DateUtils.parseDate(date2, parsePattern));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return c1.compareTo(c2) >= 0;
	}

	public static boolean isToday(String date1) {
		if (StringUtils.isBlank(date1)) {
			return false;
		}
		String c1 = date1.substring(0, 10);
		return c1.equalsIgnoreCase(getNowDate());
	}

	public static String getNowDateTime() {
		return DateFormatUtils.format(Calendar.getInstance(), YMDHMS_PATTERN);
	}

	public static String getNowDateTime1() {
		return DateFormatUtils.format(Calendar.getInstance(), YMDHMS_PATTERN1);
	}

	public static String getNowDate() {
		return DateFormatUtils.format(Calendar.getInstance(), YMD_PATTERN);
	}

	public static String getNowDateC() {
		return DateFormatUtils.format(Calendar.getInstance(), YMD_PATTERN_C);
	}

	public static String getYesterDayNowDate() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -1);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getLastYearNowDate() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, -1);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getLastYearYesterDayNowDate() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, -1);
		c.add(Calendar.DAY_OF_MONTH, -1);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getLastYearNowDateByDate(String date) {
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(DateUtils.parseDate(date, YMD_PATTERN));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.add(Calendar.YEAR, -1);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getMMDateByDate(String date) {
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(DateUtils.parseDate(date, YMD_PATTERN));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return DateFormatUtils.format(c, MM_PATTERN);
	}

	public static String getYesterDayNowDateCN() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -1);
		return DateFormatUtils.format(c, "yyyy年MM月dd日");
	}

	public static String getNowMonthFirstDayNowDateCN() {
		String date = getNowMonthFirstDay();
		try {
			Date date1 = DateUtils.parseDate(date, YMD_PATTERN);
			return DateFormatUtils.format(date1, "yyyy年MM月dd日");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;

	}

	public static String getNowNO_Date() {
		return DateFormatUtils.format(Calendar.getInstance(), YMD_NO_PATTERN);
	}

	public static String getHHDate() {
		return DateFormatUtils.format(Calendar.getInstance(), HH_PATTERN);
	}

	public static String getDDDate() {
		return DateFormatUtils.format(Calendar.getInstance(), DD_PATTERN);
	}

	public static String getMMDate() {
		return DateFormatUtils.format(Calendar.getInstance(), MM_PATTERN);
	}

	/**
	 * 获取下个月的时间
	 */
	public static String getNextMMDate() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 1);
		return DateFormatUtils.format(c, MM_PATTERN);
	}

	/**
	 * 上个月第一天
	 *
	 * @return
	 */
	public static String getLastMonthFirstDay() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		int firstDay = c.getActualMinimum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, firstDay);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	/**
	 * 这个月第一天
	 *
	 * @return
	 */
	public static String getNowMonthFirstDay() {
		Calendar c = Calendar.getInstance();
		int firstDay = c.getActualMinimum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, firstDay);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getNowMonthLastDay() {
		Calendar c = Calendar.getInstance();
		int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, lastDay);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	/**
	 * 上个月最后一天
	 *
	 * @return
	 */
	public static String getLastMonthLastDay() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, lastDay);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getLast2MonthLastDay() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -2);
		int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, lastDay);
		return DateFormatUtils.format(c, YMD_PATTERN_C);
	}

	public static String getLastMonthLastDayC() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, lastDay);
		return DateFormatUtils.format(c, YMD_PATTERN_C);
	}

	public static int getNowMonthMaxDay() {
		Calendar c = Calendar.getInstance();
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public static String getYMDDateByNum(String dayNum) {
		Calendar c = Calendar.getInstance();
		return DateFormatUtils.format(c, YM_PATTERN) + "-" + dayNum;
	}

	public static String getYMDateByNum(int monthNum) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, monthNum - 1);
		return DateFormatUtils.format(c, YM_PATTERN);
	}

	public static String getYMDateFirstDayByMonthNum(int monthNum) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, monthNum - 1);
		int firstDay = c.getActualMinimum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, firstDay);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getYMDate15ByMonthNum(int monthNum) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, monthNum - 1);
		c.set(Calendar.DAY_OF_MONTH, 15);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getYMDateLastDayByMonthNum(int monthNum) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, monthNum - 1);
		int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, lastDay);
		return DateFormatUtils.format(c, YMD_PATTERN);
	}

	public static String getLastYMDate() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		return DateFormatUtils.format(c, YM_PATTERN);
	}

	public static String getNowYMDate() {
		Calendar c = Calendar.getInstance();
		return DateFormatUtils.format(c, YM_PATTERN);
	}

	public static String getLastYMDDateByNum(String dayNum) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		return DateFormatUtils.format(c, YM_PATTERN) + "-" + dayNum;
	}

	public static int getYesterdayDateNum() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -1);
		return Integer.parseInt(DateFormatUtils.format(c, DD_PATTERN));
	}

	public static String getDateTimeByTimestamp(String timestamp) {
		return DateFormatUtils.format(new Date(Long.valueOf(timestamp + "000")), YMDHMS_PATTERN);
	}

	public static String getDateByTimestamp(String timestamp) {
		return DateFormatUtils.format(new Date(Long.valueOf(timestamp + "000")), YMD_PATTERN);
	}

	public static String getLastMonthYMC() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		return DateFormatUtils.format(c, YM_PATTERN_C);
	}

	public static void main(String[] args) {
		System.out.println(Integer.parseInt(getDDDate()));
		System.out.println(Integer.parseInt(getMMDate()));
		System.out.println(Integer.parseInt("00"));
		System.out.println(compare(null, "2019-10-31", "yyyy-MM-dd"));
		System.out.println(getLastYearNowDate());
		System.out.println(getYesterDayNowDate());
		System.out.println(getLastMonthLastDay());
		System.out.println(getLastMonthFirstDay().substring(0, 7));
		System.out.println(getYMDDateByNum("01"));

		System.out.println(getNowMonthLastDay());
		System.out.println(getLastYMDDateByNum("18"));
		System.out.println(getLastYMDate());
		System.out.println(getLastYearNowDateByDate("2019-10-12"));
		System.out.println(getNowMonthMaxDay());
		System.out.println(getYesterdayDateNum());
		System.out.println(getNowMonthFirstDay());

		// System.out.println(compare("2019-10-31", "2019-10-31",
		// DateUtilss.YMD_PATTERN));
		// System.out.println(getDateTimeByTimestamp(""));
		System.out.println(getLastMonthYMC());
		System.out.println(getLast2MonthLastDay());

		System.out.println(getYMDateFirstDayByMonthNum(4));

		System.out.println(getYMDateLastDayByMonthNum(4));

		System.out.println(Integer.parseInt("2020-04-30".substring(5, 7)));

		System.out.println(Integer.parseInt("11"));

		System.out.println(getYMDateByNum(6));

		System.out.println(getYMDate15ByMonthNum(3));

		System.out.println(getNextMMDate());

		System.out.println(compare(null, "2020-08-23", "yyyy-MM-dd"));

		System.out.println(DateUtilsx.getYMDDateByNum("23"));

		System.out.println(DateUtilsx.compare(DateUtilsx.getYMDDateByNum("24"), null, DateUtilsx.YMD_PATTERN));

		System.out.println("isToday(\"2020-11-24\") = " + isToday("2020-11-24"));

	}
}
